package com.imay.live.activity.load;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.imay.cocos.IMayCocosNetManager;
import com.imay.live.Constants;
import com.imay.live.activity.BaseCocosActivity;
import com.imay.live.activity.MainActivity2;
import com.imay.live.activity.dynamics.ReleaseActivity;
import com.imay.live.activity.user.UserActivity;
import com.imay.live.cache.AdsCache;
import com.imay.live.cache.BannerCache;
import com.imay.live.cache.PastePhotoCache;
import com.imay.live.helper.H5Helper;
import com.imay.live.manager.AccountManager;
import com.imay.live.R;

/**
 * Created by Administrator on 2017/5/21 0021.
 */

public class LoadMokeyActivty extends BaseCocosActivity {
    public static final int IN_RUNNING = 1001;
    public static final int IN_ENDING = 1002;
    private long timeCover = 2000; //总时长
    private int pertime = 25; // 每次滑动时长
    //    private long progress; // 进度
//    private int status; //1、开始图 2、封面女郎 3、其他
//    private String fileuri;
//    private RelativeLayout fl_cocos_view_container;
    private String fromurl;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_load;
    }

    ImageView imageView;
//    boolean canstart = false;//这是广告倒计时是否到 别改动 影响升级弹窗
//    boolean cosstart = false;//这是cocos 升级 别改动 影响升级弹窗


    @Override
    protected void initView(Bundle savedInstanceState) {
        imageView = $(R.id.animation_iv);
//        fl_cocos_view_container = $(R.id.fl_cocos_view_container);
         startActivity();
    }

    private void startActivity() {
        fromurl = getIntent().getStringExtra(H5Helper.H5REDIRECTION_KEY);
        // H5 回退到 后台最后一个页面
//        fromurl = ShareSDKHelper.getDataUrl(getIntent());
//        /**
//         * 后台有界面情况下，H5直接在Activity栈中最后一个界面跳
//         * 安卓按home键之后，再次点击程序图标避免再次重新启动程序解决办法
//         */
//        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
//            if(H5Helper.startActivity(fromurl)){
//                finish();
//                return;
//            }
//        }

//        addCocosView(fl_cocos_view_container, 0);

        new BannerCache(null).updateCache();
        new PastePhotoCache(null).updateCache();
//        new AdsCache(null).updateCache();


//        imageView.setImageResource(R.drawable.mokey_shake);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
//        animationDrawable.start();
        mCodeHandler.sendEmptyMessageDelayed(IN_ENDING, timeCover);
    }

    /**
     * 倒计时Handler
     */
    Handler mCodeHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == IN_RUNNING) {// 正在倒计时
                mCodeHandler.sendEmptyMessageDelayed(IN_RUNNING, pertime);
            } else if (msg.what == IN_ENDING) {
//                canstart = true;
                startHome();
            }
        }
    };


    private synchronized void startHome() {
//        if (!canstart) {//需要等lua 返回才可以跳转
//            return;
//        }
//        if (IMayCocosNetManager.getInstance().getLauncherState() == 4 && !cosstart) {
//            return;
//        }
        if (AccountManager.getInstance().isLogined()) {
            MainActivity2.startActivity(LoadMokeyActivty.this, fromurl);
        } else {
            UserActivity.startActivity(LoadMokeyActivty.this, fromurl);
        }
        finish();
    }


    @Override
    protected boolean useDynamicTitleBar() {
        return false;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (imageView != null) {
            if(imageView.getDrawable() instanceof AnimationDrawable){
                AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
                if (animationDrawable != null) {
                    animationDrawable.stop();
                }
            }
        }

        if (mCodeHandler != null) {
            mCodeHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constants.REQUEST_PICK_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    if (data == null) {
                        return;
                    }
                    Bundle b = data.getExtras();
                    String imagePath = b.getString("filePath");
                    ReleaseActivity.startActivity(this, imagePath, null, null, ReleaseActivity.IMAGE, Constants.DYNAMICSFEED_REQUEST_CODE);
                }
                break;
            case Constants.REQUEST_PICK_VIDEO:
                if (resultCode == Activity.RESULT_OK) {
                    if (data == null) {
                        return;
                    }
                    Bundle b = data.getExtras();
                    String movPath = b.getString("filePath");
                    String imgPath = b.getString("imagePath");
                    ReleaseActivity.startActivity(this, imgPath, movPath, null, ReleaseActivity.MOVIE, Constants.DYNAMICSFEED_REQUEST_CODE);
                }
                break;
        }
    }
}